package me.idashu.attg.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.idashu.attg.domain.MemberEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.service.MemberService;
import me.idashu.attg.util.Escape;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/member")
public class MemberController {
	@Autowired
	private MemberService memberService;
	/**
	 * 查询会员列表
	 * 
	 * @param pageEntity
	 * @param userEntity
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/memberList")
	public String preMemberList(Model model,HttpServletRequest request) {
		PageEntity pageEntity = new PageEntity();
		MemberEntity memberEntity = new MemberEntity();
		try {
			memberSelectMethod(pageEntity,memberEntity,request,model);
		} catch (Exception ex) {
			ex.printStackTrace();
			return "error";
		}
		return "user_list";
	}
	/**
	 * 查询会员列表公共的方法
	 * @param pageEntity
	 * @param userEntity
	 * @param request
	 * @param model
	 */
	public void memberSelectMethod(PageEntity pageEntity,MemberEntity memberEntity,HttpServletRequest request,Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		int total = memberService.getCountMemberEntityByPage(memberEntity);
		pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
		model.addAttribute("memberList", "会员列表");
		model.addAttribute("pageEntity", pageEntity);
		model.addAttribute("memberEntity", memberEntity);
		map.put("pageEntity", pageEntity);
		map.put("memberEntity", memberEntity);
		List<MemberEntity> listMemberEntity = memberService.getMemberEntityByPage(map);
		if(listMemberEntity.size()>0){
			model.addAttribute("listUserEntity", listMemberEntity);
		}else{
			model.addAttribute("listUserEntity", null);
		}
	}
	/**
     * ajax请求的用户分页列表信息
     * @param pageEntity
     * @param userEntity
     * @param model
     * @return
     */
	@RequestMapping(value = "/memberAjaxList")
	public String preAjaxUserList(@ModelAttribute PageEntity pageEntity,@ModelAttribute MemberEntity memberEntity,Model model,HttpServletRequest request) {
		try{ 
			String userName = Escape.unescape(memberEntity.getMember_Nick_Name());
			memberEntity.setMember_Nick_Name(userName);
			memberSelectMethod(pageEntity,memberEntity,request,model);
		}catch(Exception ex){
			ex.printStackTrace();
			return "error";
		}
		return "memberAjaxlist";
	}
	
}
